package com.max.cms0.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.max.cms0.entity.Slideshow;
import com.max.cms0.exception.ServiceException;
import com.max.cms0.result.ResultCode;
import com.max.cms0.service.SlideshowService;
import com.max.cms0.mapper.SlideshowMapper;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
* @author lenovo
* @description 针对表【cms_slideshow】的数据库操作Service实现
* @createDate 2024-11-18 14:40:07
*/
@Service
public class SlideshowServiceImpl extends ServiceImpl<SlideshowMapper, Slideshow>
    implements SlideshowService{
    @Autowired
    SlideshowMapper slideshowMapper ;
    @Override
    public List<Slideshow> queryAllEnable() {
        LambdaQueryWrapper<Slideshow> qw = new
                LambdaQueryWrapper<>();
        qw.eq(Slideshow::getStatus, "启用");
        qw.orderByDesc(Slideshow::getUploadTime);
        List<Slideshow> list = slideshowMapper.selectList(qw);
        if(list == null || list.size() == 0)
            throw new ServiceException(ResultCode.DATA_IS_EMPTY);
        return list;
    }

    @Override
    public boolean saveOrUpdate(Slideshow slideshow) {
        if (slideshow.getId() == null) {
            if (slideshow.getStatus() == null)
                slideshow.setStatus("启用");
            slideshowMapper.insert(slideshow);
        }else {
            if (slideshowMapper.selectById(slideshow.getId()) == null) {
                throw new ServiceException(ResultCode.DATA_IS_EMPTY);
            }else {
                slideshowMapper.updateById(slideshow);
            }
        }
        return true;
    }

    @Override
    public boolean removeById(Serializable id) {
        if (slideshowMapper.selectById(id) == null) {
            throw new ServiceException(ResultCode.DATA_IS_EMPTY);
        }else {
            slideshowMapper.deleteById(id);
        }
        return true;
    }

    public void removeByIds(List<Integer> idList) {
        slideshowMapper.deleteBatchIds(idList);
    }
}




